
Partial Class SAD_SQConnectionsL
    Inherits System.Web.UI.Page

    Private Const Q As String = Chr(34)

    Private moServer As SQLAutoDocLib.Server = Nothing
    Private moDatabase As SQLAutoDocLib.Database = Nothing

    Private msDBID As String

    Private moData As DataTable = Nothing

    Private msStartDate As String
    Private msEndDate As String

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        msDBID = Request.QueryString("D")
        msStartDate = Request.QueryString("FD")
        msEndDate = Request.QueryString("TD")

        moDatabase = New SQLAutoDocLib.Database(msDBID)
        moDatabase.Load()

        moServer = New SQLAutoDocLib.Server(moDatabase.ServerID)
        moServer.Load()

        Dim oFactory As New SQLAutoDocLib.ConnectLog_Factory
        moData = oFactory.ConnectionsInPeriod(msDBID, msStartDate, msEndDate)

        ARefresh.HRef = "SQConnectionsL.aspx?D=" & moDatabase.ID & _
                            "&FD=" & HttpUtility.UrlEncode(msStartDate) & _
                            "&TD=" & HttpUtility.UrlEncode(msEndDate)

        AZoom.HRef = "SQConnectionsE.aspx?D=" & moDatabase.ID & _
                            "&DT=" & HttpUtility.UrlEncode(CDate(msStartDate).ToShortDateString)
    End Sub

    Protected Function FormLabel() As String
        Dim oFactory As New SQLAutoDocLib.ScanHistory_Factory
        Dim sCurrentScan As String = ""
        Dim sLastScan As String = ""
        oFactory.GetTimeOfLastScan(moDatabase.ID, SQLAutoDocLib.ScanSchedule.Type.ConnectionScan, "SQLAutoDocLib.ConnectLog", sCurrentScan, sLastScan)

        Dim sValue As String = ""

        sValue = "Connection Detail for database " & moDatabase.Name & " for period " & msStartDate & " to " & msEndDate & " last scan completed " & sLastScan

        If sCurrentScan > "" Then
            sValue = " scan in progress started " & sCurrentScan
        End If

        Return sValue
    End Function

    Protected Function ScanMode() As String
        Return "Connections"
    End Function

    Protected Function EditMode() As String
        Return "Connections"
    End Function

    Protected Function ServerID() As String
        Return moServer.ID
    End Function

    Protected Sub ShowActiveConnections()
        Dim sHTM As New StringBuilder()
        With sHTM
            .Append("<table>")
            .Append("<tr>")
            .Append("<td style=" & Q & "vertical-align:bottom; text-align: center; border-bottom: solid 1px black;" & Q & ">SPID</td>")
            .Append("<td style=" & Q & "vertical-align:bottom; text-align: center; border-bottom: solid 1px black;" & Q & ">Status</td>")
            .Append("<td style=" & Q & "vertical-align:bottom; text-align: center; border-bottom: solid 1px black;" & Q & ">Host</td>")
            .Append("<td style=" & Q & "vertical-align:bottom; text-align: center; border-bottom: solid 1px black;" & Q & ">Block</td>")
            .Append("<td style=" & Q & "vertical-align:bottom; text-align: center; border-bottom: solid 1px black;" & Q & ">Command</td>")
            .Append("<td style=" & Q & "vertical-align:bottom; text-align: center; border-bottom: solid 1px black;" & Q & ">Login</td>")
            .Append("<td style=" & Q & "vertical-align:bottom; text-align: center; border-bottom: solid 1px black;" & Q & ">First Detected</td>")
            .Append("<td style=" & Q & "vertical-align:bottom; text-align: center; border-bottom: solid 1px black;" & Q & ">InDB</td>")
            .Append("</tr>")

            If Not moData Is Nothing Then
                For Each oRow As DataRow In moData.Rows
                    .Append("<tr>")
                    .Append("<td style=" & Q & "vertical-align:top; text-align: left;" & Q & ">" & oRow.Item("Connect_SPID") & "</td>")
                    .Append("<td style=" & Q & "vertical-align:top; text-align: left;" & Q & ">" & oRow.Item("Connect_Status") & "</td>")
                    .Append("<td style=" & Q & "vertical-align:top; text-align: left;" & Q & ">" & oRow.Item("Connect_HostName") & "</td>")
                    .Append("<td style=" & Q & "vertical-align:top; text-align: left;" & Q & ">" & oRow.Item("Connect_BlockID") & "</td>")
                    .Append("<td style=" & Q & "vertical-align:top; text-align: left;" & Q & ">" & oRow.Item("Connect_Command") & "</td>")
                    .Append("<td style=" & Q & "vertical-align:top; text-align: left;" & Q & ">" & oRow.Item("Connect_LoginName") & "</td>")
                    .Append("<td style=" & Q & "vertical-align:top; text-align: left;" & Q & ">" & CDate(oRow.Item("CreateDate")).ToShortDateString & "<br/>" & _
                                     CDate(oRow.Item("CreateDate")).ToShortTimeString & "</td>")
                    .Append("<td>" & oRow.Item("Connect_Current") & "</td>")
                    .Append("</tr>")
                Next
            End If

            .Append("</table>")
        End With

        Response.Write(sHTM.ToString)
    End Sub
End Class
